﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

public static class SqlClientExtension
{
    public static List<object> ToList(this DataTable data, Func<DataRow, object> predicate)
    {
        List<object> list = new List<object>();
        try
        {
            if (data.Rows.Count > 0)
            {
                foreach (DataRow dr in data.Rows)
                {
                    var obj = predicate(dr);
                    list.Add(obj);
                }
            }
        }
        catch (Exception)
        {
            
            throw;
        }
        return list;
    }
    public static object First(this DataTable data, Func<DataRow, object> predicate)
    {
        object obj = new object();
        try
        {
            if (data.Rows.Count > 0)
            {
                obj = predicate(data.Rows[0]);
                return obj;
            }
        }
        catch (Exception)
        {
        }
        return obj;
    }
    public static DateTime GetDateTime(this DataRow dr, string column, DateTime ValueDefault)
    {
        DateTime obj = (dr[column] is DBNull) ? ValueDefault : DateTime.Parse(dr[column].ToString());     
        return obj;
    }
    #region DataRow
    public static int GetInt(this DataRow dr, string column, int ValueDefault)
    {
        int obj = (dr[column] is DBNull) ? ValueDefault : int.Parse(dr[column].ToString());
        return obj;
    }
    public static float GetFloat(this DataRow dr, string column, float ValueDefault)
    {
        float obj = (dr[column] is DBNull) ? ValueDefault : float.Parse(dr[column].ToString());
        return obj;
    }
    public static long GetLong(this DataRow dr, string column, long ValueDefault)
    {
        long obj = (dr[column] is DBNull) ? ValueDefault : long.Parse(dr[column].ToString());
        return obj;
    }
    public static Boolean GetBoolean(this DataRow dr, string column, Boolean ValueDefault)
    {
        Boolean obj = (dr[column] is DBNull) ? ValueDefault : Boolean.Parse(dr[column].ToString());
        return obj;
    }
    public static string GetString(this DataRow dr, string column, string ValueDefault)
    {
        string obj = (dr[column] is DBNull) ? ValueDefault : dr[column].ToString();
        return obj;
    }
    #endregion
}

